import { observable, action, toJS } from 'mobx';
import { message } from 'antd';
import { WeaTableNew } from 'comsMobx'
import * as Api from '../../apis/groupvoting';
import NewGroupVotingStore from './NewGroupVoting';

const { TableStore } = WeaTableNew;
const initalState = {
  newVisible: false,
  isShowDetail: false,
  listLoading: true,
  isSearch: false,
  dataInfo: {},
  selectedKeys: [],
  voteId: '',
  saveLoading: false,
  detailLoading: true,
  groupid: '',
  isgroup: '',

  personVisible: false,
  usertype: '',
  personTitle: '',
}
export default class GroupListStore {
  @observable newGroupVotingStore = new NewGroupVotingStore();
  @observable listStore = new TableStore();
  @observable personListStore = new TableStore();
  @observable newVisible = initalState.newVisible;
  @observable isShowDetail = initalState.isShowDetail;
  @observable isSearch = initalState.isSearch; 
  @observable dataInfo = initalState.dataInfo;
  @observable selectedKeys = initalState.selectedKeys;
  @observable voteId = initalState.voteId;
  @observable saveLoading = initalState.saveLoading;
  @observable detailLoading = initalState.detailLoading;
  @observable groupid = initalState.groupid;
  @observable isgroup = initalState.isgroup;

  // 人员列表状态
  @observable personVisible = initalState.personVisible;
  @observable usertype = initalState.usertype;
  @observable personTitle = initalState.personTitle;

  @action setStoreState = (params, callback) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    })
    if (callback) callback();
  }

  /* 列表请求 */
  @action getGroupVoteList = (params, reloadPage = false, callback) => {
    this.listLoading = true;
    params = {
      groupid: this.groupid,
      isgroup: this.isgroup,
      ...params
    }
    Api.getGroupVoteList(params).then(res => {
      reloadPage ? this.listStore.getDatas(res.sessionkey, '', '', '', () => {
        if (callback && typeof callback === 'function') callback();
      }) : 
      this.listStore.getDatas(res.sessionkey, 1, '', '', () => {
        if (callback && typeof callback === 'function') callback();
      });
      this.listLoading = false;
    })
  }

  /* 删除群投票 */
  @action groupchatvotedel = (params) => {
    Api.groupchatvotedel(params).then(res => {
      if (res.api_status === 'success') {
        this.getGroupVoteList('', true);
        message.success('删除成功！');
      } else {
        message.error('删除失败，请重试！');
      }
    })
  }

  /* 批量分享 */
  @action groupchatvoteshare = (params) => {
    Api.groupchatvoteshare(params).then(res => {
      
    })
  }

  /* 详情 */
  @action viewGroupVote = (params) => {
    this.detailLoading = true;
    params = {groupid: this.groupid, ...params}
    Api.viewGroupVote(params).then(res => {
      if (res.votedata) {
        this.dataInfo = res.votedata;
      } else {
        message.error('请求有误，请重试！');
      }
      this.detailLoading = false;
    })
  }

  // 投票
  @action onVote = (params, callback) => {
    let select = toJS(this.selectedKeys);
    let data = toJS(this.dataInfo);
    if (select.length === 0) {
      message.error('请选择！');
      return;
    }

    if (data.choosemodel === '1' && (Number(data.maxvoteoption) < select.length)) {
      message.error('您已超出最大可选项数！');
      return;
    }

    this.saveLoading = true;
    params = {
      ...params,
      votingid: this.voteId,
      votooptioncheck: select.join(),
    }
    Api.savegroupvoteresult(params).then(res => {
      if (res.status === '1') {
        message.success('投票成功！');
        this.viewGroupVote({votingid: this.voteId});
        if (callback && typeof callback === 'function') callback(res);
      }
      this.saveLoading = false;
    })
  }

  /************************************   投票人员--列表   ************************************/ 
  @action groupchatvoteprelist = (params) => {
    params ={votingid: this.voteId, usertype: this.usertype, groupid: this.groupid, ...params}
    Api.groupchatvoteprelist(params).then(res => {
      this.personListStore.getDatas(res.sessionkey, 1);
    })
  }
}